//
//  MainScrollView.m
//  zhongwulegou
//
//  Created by qianfeng on 15/9/17.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "MainScrollView.h"
#import "UIImageView+AFNetworking.h"
#import "MainScrollModel.h"

@implementation MainScrollView

-(void)makeScrollViewWithPictureNum:(int)num andPictureArray:(NSArray *)array
{
    
    if (array.count > 0) {
        NSMutableArray *pictureArray = [NSMutableArray new];
        for (MainScrollModel *model in array) {
            [pictureArray addObject:model.imageURL];
        }
        
        UIScrollView *_mainScrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        _mainScrollView.delegate = self;
        _mainScrollView.contentSize = CGSizeMake(self.frame.size.width * (num + 2), self.frame.size.height);
        _mainScrollView.pagingEnabled = YES;
        [self addSubview:_mainScrollView];
        
        //添加图片
        for (int i = 0; i < num+2 ; i++) {
            UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(i*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
            if (i ==0 ) {
                [imgView setImageWithURL:[NSURL URLWithString:pictureArray[num-1]]];
            }else if (i == num+1){
                [imgView setImageWithURL:[NSURL URLWithString:pictureArray[0]]];
            }else{
                [imgView setImageWithURL:[NSURL URLWithString:pictureArray[i-1]]];
            }
            [_mainScrollView addSubview:imgView];
        }
        
        //默认显示第二张
        _mainScrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
        
        //Page 视图
        UIPageControl *page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.frame.size.height-30, self.frame.size.width, 20)];
        page.numberOfPages = num;
        page.backgroundColor = [UIColor clearColor];
        page.pageIndicatorTintColor = [UIColor lightGrayColor];
        page.currentPageIndicatorTintColor = [UIColor whiteColor];
        page.alpha = 0.5;
        
        [self addSubview:page];
        
        _mainScrollView.tag = 301;
        page.tag = 300;
        
        _timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(scroll:) userInfo:nil repeats:YES];
        
        _count = 1;
        _num = num;
    }
}

-(void)scroll:(NSTimer *)timer
{
    _count++;
    UIScrollView *_mainScrollView = (UIScrollView *)[self viewWithTag:301];
    
    if (_count >= _num+1) {
        _count = 1;
        _mainScrollView.contentOffset = CGPointMake(0, 0);
    }
    [_mainScrollView setContentOffset:CGPointMake(_count * self.frame.size.width, 0) animated:YES];
    
    UIPageControl *page = (UIPageControl *)[self viewWithTag:300];
    page.currentPage = _count - 1;
    
}

#pragma mark -UIScrollViewDelegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [_timer invalidate];
    UIScrollView *scroll = (UIScrollView *)[self viewWithTag:301];
    _count = scroll.contentOffset.x/self.frame.size.width - 1;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(scroll:) userInfo:nil repeats:YES];
}



@end
